<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../WEB-INF/jsp/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-注册</title>
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/reset.css">
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/main.css">
	<script type="text/javascript" src="${path}/static/front/js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript" src="${path}/static/front/js/register.js"></script>
</head>
<body>
	<div class="register_con">
		<div class="l_con fl">
			<a class="reg_logo"><img src="${path}/static/front/images/logo02.png"></a>
			<div class="reg_slogan">足不出户  ·  新鲜每一天</div>
			<div class="reg_banner"></div>
		</div>

		<div class="r_con fr">
			<div class="reg_title clearfix">
				<h1>用户注册</h1>
				<a href="${path}/login.jsp">登录</a>
			</div>
			<div class="reg_form clearfix">
			<form id="register_form" method="post" onsubmit="return false">
				<ul>
					<li>
						<label>用户名:</label>
						<input type="text" name="username" id="usernameId">
						<span class="error_tip">提示信息</span>
					</li>					
					<li>
						<label>密码:</label>
						<input type="password" name="password" id="pwdId">
						<span class="error_tip">提示信息</span>
					</li>
					<li>
						<label>确认密码:</label>
						<input type="password" name="cpassword" id="cpwdId">
						<span class="error_tip">提示信息</span>
					</li>
					<li>
						<label>邮箱:</label>
						<input type="text" name="email" id="emailId">
						<span class="error_tip">提示信息</span>
					</li>
					<li class="agreement">
						<input type="checkbox" name="allow" id="allow" checked="checked">
						<label>同意”天天生鲜用户使用协议“</label>
						<span class="error_tip2">提示信息</span>
					</li>
					<li class="reg_sub">
						<input type="submit" value="注 册" name="" onclick="submitForm()">
					</li>
				</ul>				
				</form>
			</div>

		</div>

	</div>

	<div class="footer no-mp">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	
	<script src="${path}/static/lib/jquery-1.11.3.js" type="text/javascript" charset="utf-8"></script>
    <script src="${path}/static/lib/layer/layer.js" type="text/javascript" charset="utf-8"></script>
    <script src="${path}/static/lib/mylayer.js" type="text/javascript" charset="utf-8"></script>
    <script src="${path}/static/lib/myUtil.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">    	
    	function submitForm() {
    		console.log("submitForm");
			// 1、	验证用户名
			// 1.1	用户名不能为空
			// 1.2	格式是否合法：4-8位数字或字母
			var name = $("#usernameId").val();
			var email = $("#emailId").val();
			var password = $("#pwdId").val();
			var cpassword = $("#cpwdId").val();
			// 1.1	用户名不能为空
			if (myUtil.isEmpty(name)) {
				mylayer.errorMsg("用户名不能为空");
				return;
			}
			// 1.2	格式是否合法：5-20位数字或字母
			var pattern = /^[0-9a-zA-Z]{5,20}$/;
			// if (!pattern.test(name)) {
			if (pattern.test(name) == false) {
				mylayer.errorMsg("用户名格式非法");
				return;
			}
			
			if (myUtil.isEmpty(email)) {
				mylayer.errorMsg("邮箱不能为空");
				return;
			}
			// 1.2	格式是否合法
			var pattern = /\w+@\w+(\.\w+)+/;
			if (pattern.test(email) == false) {
				mylayer.errorMsg("邮箱格式非法");
				return;
			}
			
			// 2、密码验证
			// 2.1	用户名不能为空
			// 2.2	格式是否合法
			
			// 2.1	用户名不能为空
			if (myUtil.isEmpty(password)) {
				mylayer.errorMsg("密码不能为空");
				return;
			}
			// 2.2	格式是否合法
			var pattern = /^[0-9a-zA-Z]{3}$/;
			// if (!pattern.test(password)) {
			if (pattern.test(password) == false) {
				mylayer.errorMsg("密码格式非法，3位数字或字母");
				return;
			}
			
			if(cpassword!=password){
				mylayer.errorMsg("两次密码不一致");
				return;
			}
			
			// 3、ajax提交用户名和密码到后台，验证是不是存在，返回ServerResponse对象，
			// 前台根据返回的ServerResponse决定响应的操作
			console.log($("#register_form").serialize());
			$.ajax({
				url : "${path}/user/register.shtml",
				type : "post",
				dataType : "json",
				data : $("#register_form").serialize(),
				success : function(jsonObj) {
					// {code:1,msg:"注册成功"}					
					if(jsonObj.code == myUtil.SUCCESS) {
						mylayer.successMsgUrl(jsonObj.msg, "${path}/login.jsp");
					} else {
						// {"code":0,"count":0,"data":null,"msg":"注册错误"}
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			});
    	}
	</script>
</body>
</html>